package com.ay.controller;


import com.ay.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @RequestMapping("/tologin")
    public String find1(Model model){
        model.addAttribute("msg","请输入用户名和密码");

        return "login";
    }

    @RequestMapping("/ceshi")
    public String  ceshi(){
        return "ceshi";
    }

    //后端用对象接收,前端会自动将参数封装成对象,但是容器中要有这个类的Bean,并且参数名字相同
    @RequestMapping("/login")
    public String find2(User user, Model model, HttpSession session){
        System.out.println(user.getUsername());
        System.out.println(user);
        if("lxjash".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            session.setAttribute("user",user);
            return "success";
        }


        //防止用户直接输入http://localhost:8080/login
        if(user.getUsername()==null&&user.getPassword()==null){
            model.addAttribute("msg","请输入用户名和密码");
            return "login";

        }

        model.addAttribute("msg","用户名或密码错误,请重新登录");
        return "login";



    }

    @RequestMapping("/logout")
    public String find3(HttpSession session){

        //清楚session
       session.invalidate();

        return "login";
    }
}
